// JavaScript Document

function getIconList ( type, successFunction ) {
	var uri = store_rest_url+"/creator/icons/"+type;
	$.ajax({
		type: "GET",
		url: uri,
		beforeSend: function(xhr) {
			var authString = getBase64Authentication();
			xhr.setRequestHeader("Authorization", "Basic "+authString);
		},
		dataType: "text",
		success: successFunction
	});
}




function buildIconChooser ( iconList, divId ) {
	var icons = $.parseJSON(iconList);
	var radioSet = "";
	var radioName = divId;
	for ( var i = 0; i < icons.length; i++ ) {
		var icon = icons[i];
		var imageFile = icon.substring(icon.lastIndexOf('/')+1);
		var selected = "";
		var imageTitle = createTitleFromIconFileName ( imageFile );
		if ( imageFile.indexOf("default") != -1 ) {
			selected = 'checked="checked"';
		}
		radioSet += '<input type="radio" id="'+imageFile+'" name="icon" value="'+imageFile+'" '+selected+' />'
		radioSet += '<label for="'+imageFile+'">';
		radioSet += '<img src="'+icons[i]+'" width="32" height="32" style="widget_icon" title="'+imageTitle+'"/> ';
		radioSet += '</label>';
	}
	$("#"+divId).html(radioSet);
	$("#"+divId).buttonset();
}


String.prototype.capitalize = function() {
	return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
}

function createTitleFromIconFileName ( iconFileName ) {
	var arr = iconFileName.split('_');
	return arr[0].capitalize() + " " + arr[1].capitalize();
}

